package CS4310.csueb.main;

///////////////////////////////////////////////////////////////////////////////////
//"software engineering 1" project: game of choice                                                               
//Driver class with initial GUI layer                                                                           
//(includes menu bar and blank content pane below it)                                                                                                                                                                                           //
///////////////////////////////////////////////////////////////////////////////////

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import CS4310.csueb.characters.*;
import CS4310.csueb.items.*;
import CS4310.csueb.visual.*;
import CS4310.csueb.characters.Hero;

///////////////////////////// class gameFrame /////////////////////////////////////
// creates main window, contains main method
public class GameFrame extends JFrame implements ActionListener {
	// private JFrame frame; // frame to house content and menu bar
	private GameManager game; // content pane that sits in main frame
	private JMenuBar menubar; // object menu bar
	private JMenu Gamemenu; // menu bar's first drop down menu "Game"
	private JMenu characterMenu; // menu bar's second drop down menu "Character"

	private JLabel hprog, eprog, musicL;
	private JRadioButton musicOn, musicOff;
	private JPanel cPane;
	private JButton gameMenu, gameBio, gameBattle, gmaeTitle;
	private JProgressBar hProgress, eProgress;

	private static final String GAMETITLE = "2753"; // constant title on the
													// frame
	// FOR TESTING FUNCTIONALITY WITH A CONSTANT HERO (AS OPPOSED TO CALLING NEW
	// EACH TIME)
	private HeroSprite h = new HeroSprite(
			"CS4310/csueb/visual/img/Male_Right_BattleFrame.png", "Bill Westin");

	// ************* constructor ***************//
	private GameFrame() {
		super(GAMETITLE); // create frame with a title
		setLayout(null);

		// set up frame
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setPreferredSize(new Dimension(800, 600)); // set size to 600px by 800px
		this.setResizable(false); // frame size is fixed

		//makeMenu(); // creates menu bar
		createGUI();  // the content pane
		toolBar();  // tool bar

		// display frame
		this.pack();
		this.setVisible(true);
		this.setLocationRelativeTo(null); // Centers window on screen

		// When default close is clicked, display a confirm dialog box
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				confirmExit();
			}
		});

		  //really ugly way to set the frame as unmovable (center window each time it is moved) 
		   this.addComponentListener(new ComponentListener() {
		  		public void componentMoved(ComponentEvent e){setLocationRelativeTo(null);} 
		  		public void componentHidden(ComponentEvent arg0){}
		  		public void componentResized(ComponentEvent arg0) {}
		  		public void componentShown(ComponentEvent arg0) {}
		  });
		 

	}

	// ************** createGUI() ***************//
		private void createGUI() {
			// create and set up content pane
			game = new GameManager();
			game.setBounds(0, 0, 800, 500);

			// add the content pane to the main frame
			this.add(game);
		}

	
	// **************toolBar()****************//
	// Please Check the PATH
	private void toolBar() {
		cPane = new JPanel();
		cPane.setLayout(null);
		// cPane.setPreferredSize(new Dimension(800,80));
		cPane.setBounds(0, 500, 800, 80);
		cPane.setBackground(Color.WHITE);

		// progress bar
		hProgress = new JProgressBar();
		int healthP = h.getHeroSheet().getCurrHP();
		hProgress.setMinimum(0);
		hProgress.setMaximum(h.getHeroSheet().getMaxHP());
		hProgress.setValue(healthP);
		hProgress.setBounds(3, 25, 250, 15);
		hProgress.setForeground(Color.GREEN);
		hProgress.setBackground(Color.WHITE);
		hProgress.setBorderPainted(true);

		// updateBars();

		// Experience Progress Bar
		eProgress = new JProgressBar();
		int exprP = h.getHeroSheet().getExp();
		eProgress.setMinimum(0);
		eProgress.setMaximum(100);
		eProgress.setValue(exprP);
		eProgress.setBounds(3, 55, 250, 15);
		eProgress.setForeground(Color.GREEN);
		if (exprP <= 80 && exprP > 50) {
			eProgress.setForeground(Color.ORANGE);
		} else if (exprP <= 50 && exprP > 20) {
			eProgress.setForeground(Color.YELLOW);
		} else if (exprP <= 20) {
			eProgress.setForeground(Color.RED);

		}

		// labels

		// Health Progress
		hprog = new JLabel("Health   " + h.getHeroSheet().getCurrHP() + "\\"
				+ h.getHeroSheet().getMaxHP());
		hprog.setBounds(3, 10, 100, 20);
		
		//used if Box Layout
		//hprog.setVerticalAlignment(JLabel.TOP);
		//hprog.setHorizontalAlignment(JLabel.LEFT);

		// Experience Progress
		eprog = new JLabel("Experience   " + h.getHeroSheet().getExp());
		eprog.setBounds(3, 40, 100, 20);
		
		//used if Box Layout
		//eprog.setVerticalAlignment(JLabel.TOP);
		//hprog.setHorizontalAlignment(JLabel.LEFT);

		/*
		 * Music Label musicL = new JLabel("Music:");
		 * musicL.setBounds(730,-21,100,100);
		 */

		// HERO -- IMAGE on the Panel -- decoration NO functionality
		ImageIcon heroIcon = new ImageIcon(
				"CS4310/csueb/visual/img/game_title.gif");
		gmaeTitle = new JButton(heroIcon);
		gmaeTitle.setBounds(300, 2, 70, 70);
		gmaeTitle.setBorderPainted(false);
		gmaeTitle.setRolloverEnabled(false);
		gmaeTitle.setBackground(Color.white);

		// buttons
		ImageIcon b1 = new ImageIcon("CS4310/csueb/visual/img/GameMenu.gif");
		ImageIcon b1r = new ImageIcon(
				"CS4310/csueb/visual/img/GameMenuRoll.gif");
		gameMenu = new JButton(b1);
		gameMenu.setBorderPainted(false);
		gameMenu.setRolloverIcon(b1r);
		gameMenu.setPressedIcon(b1r);
		gameMenu.setActionCommand("GameMenu");
		gameMenu.addActionListener(this);
		gameMenu.setBounds(450, 15, 50, 50);

		ImageIcon b2 = new ImageIcon("CS4310/csueb/visual/img/bio.gif");
		ImageIcon b2r = new ImageIcon("CS4310/csueb/visual/img/bioRoll.gif");
		gameBio = new JButton(b2);
		gameBio.setBorderPainted(false);
		gameBio.setRolloverIcon(b2r);
		gameBio.setPressedIcon(b2r);
		gameBio.setActionCommand("Bio");
		gameBio.addActionListener(this);
		gameBio.setBounds(550, 15, 50, 50);

		ImageIcon gb = new ImageIcon("CS4310/csueb/visual/img/sword.png");
		ImageIcon gbr = new ImageIcon("CS4310/csueb/visual/img/sword.png");
		gameBattle = new JButton(gb);
		gameBattle.setBorderPainted(false);
		gameBattle.setRolloverIcon(gbr);
		gameBattle.setPressedIcon(gb);
		gameBattle.setActionCommand("Battle");
		gameBattle.addActionListener(this);
		gameBattle.setBounds(650, 15, 50, 50);

		// music toggle
		// background music if we ever consider having it!
		// music on
		/*
		 * musicOn = new JRadioButton("On"); musicOn.setBackground(Color.white);
		 * musicOn.setBounds(730,40,100,20); //music off musicOff = new
		 * JRadioButton("Off"); musicOff.setBackground(Color.white);
		 * musicOff.setBounds(730,60,100,20);
		 * 
		 * //group radio buttons ButtonGroup group = new ButtonGroup();
		 * group.add(musicOn); group.add(musicOff);
		 */
		// add to Panel
		cPane.add(hprog);
		cPane.add(hProgress);
		cPane.add(eprog);
		cPane.add(eProgress);
		// buttons
		cPane.add(gmaeTitle); // Game Title on the toolbar
		cPane.add(gameMenu); // Dialouge
		cPane.add(gameBio); // Game Bio
		cPane.add(gameBattle);

		/*
		 * MUSIC toggle add to PANEL cPane.add(musicL); cPane.add(musicOn);
		 * cPane.add(musicOff);
		 */

		// add Panel to frame
		this.add(cPane);
	}

	
	
	@Override
	//****************** actionPerformed *****************//
	public void actionPerformed(ActionEvent arg0) {
		// launches a new bio frame when asked to
		if (arg0.getActionCommand().equals("Bio"))
			new BioFrame(h.getHeroSheet());
		else if (arg0.getActionCommand().equals("Battle")) {
			BattleFrame b = new BattleFrame(this, h, new CharacterSprite(
					"CS4310/csueb/visual/img/Male_Left_BattleFrame.png",
					new Enemy("Folvani Warrior")));

			b.addWindowListener(new WindowAdapter() {
				public void windowClosed(WindowEvent e) {
					updateBars();
				}
			});
			if (h.getHeroSheet().getCurrHP() == 0) {
				gameOverScreen();
			}
		} else if (arg0.getActionCommand().equals("GameMenu"))
			gameMenu();
		else if (arg0.getActionCommand().equals("exit game"))
			confirmExit();
	}

	
	//***************** updateBars()**************//
	protected void updateBars() {
		// TODO Auto-generated method stub
		int heroHealth = h.getHeroSheet().getCurrHP();
		int maxHP = h.getHeroSheet().getMaxHP();
		hprog.setText("Health   " + heroHealth + "\\" + maxHP);
		hProgress.setValue(heroHealth);

		if (heroHealth <= Math.ceil(maxHP * 0.8)
				&& heroHealth >= Math.ceil(maxHP / 2)) {
			hProgress.setForeground(Color.YELLOW);
		} else if (heroHealth < Math.ceil(maxHP / 2)
				&& heroHealth >= Math.ceil(maxHP / 4)) {
			hProgress.setForeground(Color.ORANGE);
		} else if (heroHealth < Math.ceil(maxHP / 4)) {
			hProgress.setForeground(Color.RED);
		}

		cPane.revalidate();
	}
	

	//********************* gmaeMenu() ********************//
	private void gameMenu() {
		Object[] options = { "New Game", "Load Game", "Save Game", "Exit Game" }; // optional
																					// buttons
		int optAvail = JOptionPane.showOptionDialog(null,
				"2753\nSave the game before you Exit!", "Game Menu",
				JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
				null, options, options[0]);

		if (optAvail == JOptionPane.YES_OPTION) {
			confirmRestart();
		}// new game
		else if (optAvail == JOptionPane.NO_OPTION) {
		}// Load
		else if (optAvail == JOptionPane.CANCEL_OPTION) {
		}// Save
		else if (optAvail == JOptionPane.QUESTION_MESSAGE) {// Exit
			confirmExit();
		}

	}

	//************ confirmExit() *************//
	private void confirmExit() {
		int proceed = JOptionPane.showConfirmDialog(this,
				"Exiting will cause you to lose any unsaved data.\nLeave now?",
				"Are you sure?", JOptionPane.OK_CANCEL_OPTION);
		if (proceed == JOptionPane.OK_OPTION)
			System.exit(0);
	}

	
	//*************** confirmRestart() ************//
	private void confirmRestart() {
		// If new game option is chosen
		int proceed = JOptionPane
				.showConfirmDialog(
						this,
						"Starting a new game will cause you to lose any unsaved data.\nLeave now?",
						"Are you sure?", JOptionPane.OK_CANCEL_OPTION);
		if (proceed == JOptionPane.OK_OPTION) {
			this.dispose();
			new GameFrame();
		}
	}
	

	//************ gameOverScreen() ************//
	private void gameOverScreen() {
		// character has died after battle
		int proceed = JOptionPane.showConfirmDialog(this,
				"Game Over\nDo you want to start a new game?", "GAME OVER!",
				JOptionPane.YES_NO_OPTION);
		if (proceed == JOptionPane.YES_OPTION) {
			this.dispose();
			new GameFrame();
		} else
			System.exit(0);
	}

	// ************** makeMenu() **************//
		/*
		 * private void makeMenu() { menubar = new JMenuBar();
		 * menubar.setPreferredSize(new Dimension(800,20)); //menubar.setSize(800,
		 * 20); menubar.setBounds(0, 0, 800, 20);
		 * 
		 * Gamemenu = new JMenu("Game"); // create menu "Game"
		 * menubar.add(Gamemenu); // add the Game menu to menubar characterMenu =
		 * new JMenu("Character"); menubar.add(characterMenu);
		 * 
		 * JMenuItem menuitem; // used to hold menu items
		 * 
		 * // New Game menu item menuitem = new JMenuItem("New Game");
		 */

		/*
		 * menuitem.addActionListener(new ActionListener() {
		 * 
		 * public void actionPerformed(ActionEvent e) {
		 * 
		 * //newgame = new gameFrame(); //contentpane.add(newgame); //try {
		 * //newgame.setSelected(true); //} catch (java.beans.PropertyVetoException
		 * pve) {} } });
		 */
		/*
		 * 
		 * Gamemenu.add(menuitem);
		 * 
		 * // Load Game menu item menuitem = new JMenuItem("Load Game"); // ...
		 * Gamemenu.add(menuitem);
		 * 
		 * // Save Game menu item menuitem = new JMenuItem("Save Game"); // ...
		 * Gamemenu.add(menuitem);
		 * 
		 * // Game Options menu item // ...
		 * 
		 * // Exit menu item menuitem = new JMenuItem("Exit");
		 * menuitem.setActionCommand("exit game"); menuitem.addActionListener(this);
		 * Gamemenu.addSeparator(); Gamemenu.add(menuitem);
		 * 
		 * menuitem = new JMenuItem("Bio"); menuitem.setActionCommand("Bio");
		 * menuitem.addActionListener(this); characterMenu.add(menuitem);
		 * 
		 * menuitem = new JMenuItem("Battle"); menuitem.setActionCommand("Battle");
		 * menuitem.addActionListener(this); characterMenu.add(menuitem);
		 * 
		 * this.setJMenuBar(menubar); }
		 */

	
	//////////////////////////////////////////////////////
	///*************** method main() ******************///
	public static void main(String[] argv) {
		// invoke thread after EDT clear
		// thread runs gameFrame

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new GameFrame();
				// creates this game's window
			}
		});
	}
} // end class GameFrame